#include "StdAfx.h"
#include "util.h"

using namespace SocketCtrl4;

bool SocketCtrl4::splitSockAddr(const sockaddr &addr, std::string &ip, int &port)
{
	const sockaddr_in *addrIn = (const sockaddr_in *)&addr;

	port = ntohs(addrIn->sin_port);
	ip = ::inet_ntoa(addrIn->sin_addr);

	return true;
}

bool SocketCtrl4::fillSockAddr(sockaddr &addr, const std::string &ip, int port)
{
	::ZeroMemory(&addr, sizeof(addr));

	sockaddr_in *addrIn = (sockaddr_in *)&addr;

	addrIn->sin_family = AF_INET;
	addrIn->sin_port = htons(port);
	addrIn->sin_addr.S_un.S_addr = ::inet_addr(ip.c_str());

	return true;
}

unsigned int SocketCtrl4::ip_str2net(const std::string &ip)
{
	return inet_addr(ip.c_str());
}

unsigned int SocketCtrl4::ip_str2host(const std::string &ip)
{
	return SocketCtrl4::ip_net2host(SocketCtrl4::ip_str2net(ip));
}

std::string SocketCtrl4::ip_net2str(unsigned int ip)
{
	in_addr v;
	v.S_un.S_addr = ip;
	return inet_ntoa(v);
}

std::string SocketCtrl4::ip_host2str(unsigned int ip)
{
	return SocketCtrl4::ip_net2str(SocketCtrl4::ip_net2host(ip));
}

unsigned int SocketCtrl4::ip_host2net(unsigned int ip)
{
	return htonl(ip);
}

unsigned int SocketCtrl4::ip_net2host(unsigned int ip)
{
	return ntohl(ip);
}